package cn.iocoder.yudao.module.dine.print.service.admin;

import cn.iocoder.yudao.framework.business.basic.pojo.dto.SimpleQueryDTO;
import cn.iocoder.yudao.framework.business.basic.service.PageService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.dine.print.convert.DinePrintConfigConvert;
import cn.iocoder.yudao.module.dine.print.dao.DinePrintConfigMapper;
import cn.iocoder.yudao.module.dine.print.pojo.dto.DinePrintConfigSaveDTO;
import cn.iocoder.yudao.module.dine.print.pojo.po.DinePrintConfig;
import cn.iocoder.yudao.module.dine.print.pojo.vo.DinePrintConfigVO;
import cn.iocoder.yudao.module.dine.print.service.core.CoreDinePrintConfigService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/08/22
 */
@Service
public class DinePrintConfigService extends BaseService<DinePrintConfig, DinePrintConfigVO, SimpleQueryDTO> implements PageService<DinePrintConfigVO, SimpleQueryDTO> {
    @Resource
    private DinePrintConfigMapper dinePrintConfigMapper;

    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private CoreDinePrintConfigService coreDinePrintConfigService;

    @Override
    public DinePrintConfigMapper getInjectedMapper() {
        return dinePrintConfigMapper;
    }

    @Override
    public DinePrintConfigVO convertToViewObject(DinePrintConfig entity) {
        return DinePrintConfigConvert.INSTANCE.toViewObject(entity);
    }

    @DataPermission(enable = false)
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(Long shopId, DinePrintConfigSaveDTO param) {
        DinePrintConfig entity = DinePrintConfigConvert.INSTANCE.toEntity(param);
        entity.setShopId(shopId);
        DinePrintConfig config = coreDinePrintConfigService.getOrCreate(shopId);
        entity.setId(config.getId());
        return dinePrintConfigMapper.updateById(entity) > 0;
    }

}
